package com.neusoft.SPNursingHome.service;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neusoft.SPNursingHome.dao.BedDao;
import com.neusoft.SPNursingHome.dao.ClientDao;
import com.neusoft.SPNursingHome.dao.OutingRecordDao;
import com.neusoft.SPNursingHome.po.*;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;

@Service
public class OutingRecordServiceImpl implements OutingRecordService {
    @Resource
    private OutingRecordDao outingRecordDao;
    @Resource
    private ClientDao clientDao;
    @Resource
    private BedDao bedDao;

    @Override
    public ResponseBean<IPage<OutingRecord>> listRequests(long pageNum, long pageSize, Map<String, Object> query) {
        Page<OutingRecord> page = new Page<>(pageNum, pageSize);
        IPage<OutingRecord> resultPage = outingRecordDao.findOutingRecordsPage(page, query);
        return new ResponseBean<>(resultPage);
    }

    @Override
    public ResponseBean<Void> applyForOuting(OutingRecord request) {
        // 从传入的 OutingRecord 对象中设置默认值
        request.setOutReviewStatus("已提交"); // 已提交 (默认状态)
        outingRecordDao.insert(request);
        return new ResponseBean<>(200, "外出申请提交成功");
    }

    @Override
    @Transactional
    public ResponseBean<Void> auditRequest(OutingRecord auditInfo) {
        Integer checkOutId = auditInfo.getOutId(); // 获取要审核的外出记录ID
        Integer adminId = auditInfo.getAdId(); // 获取审核人ID
        // 1. 获取数据库中完整的、原始的申请记录
        OutingRecord recordToUpdate = outingRecordDao.selectById(checkOutId);
        if (recordToUpdate == null) throw new RuntimeException("申请记录不存在");

        // 2. 使用 auditInfo 中的数据更新记录
        recordToUpdate.setOutReviewStatus(auditInfo.getOutReviewStatus());
        recordToUpdate.setOutDescription(auditInfo.getOutDescription());
        recordToUpdate.setAdId(adminId); // 审核人是当前管理员
        recordToUpdate.setOutReviewTime(new Date());
        outingRecordDao.updateById(recordToUpdate);

        // 3. 如果审核通过且为特定类型，则执行联动操作
        boolean isApproved = "通过".equals(auditInfo.getOutReviewStatus()); // 通过

        if (isApproved) {
            Client client = clientDao.selectById(recordToUpdate.getClientId());
            if (client != null && client.getBedId() != null) {
                // a. 修改床位状态为外出
                Bed bedToUpdate = new Bed();
                bedToUpdate.setBedId(client.getBedId());
                bedToUpdate.setBedStatus("外出");
                bedDao.updateById(bedToUpdate);
            }
        }
        return new ResponseBean<>(200, "审核操作完成");
    }

    @Override
    @Transactional // *** 确保所有数据库操作的原子性 ***
    public ResponseBean<Void> recordClientReturn(Integer outingId, Date returnTime) {
        // 1. 查找并更新外出记录
        OutingRecord record = outingRecordDao.selectById(outingId);
        if (record == null) {
            return new ResponseBean<>(404, "找不到对应的外出记录");
        }
        record.setReturnTime(returnTime);
        outingRecordDao.updateById(record);

        // 2. 查找客户信息以获取床位ID
        Client client = clientDao.selectById(record.getClientId());
        if (client == null || client.getBedId() == null) {
            // 即使客户或床位信息缺失，外出记录也已更新，所以操作不算完全失败
            return new ResponseBean<>(200, "返回时间记录成功，但未找到关联的客户或床位信息。");
        }

        // 3. 更新床位状态为 "有人"
        Bed bedToUpdate = new Bed();
        bedToUpdate.setBedId(client.getBedId());
        bedToUpdate.setBedStatus("有人");
        bedDao.updateById(bedToUpdate);

        return new ResponseBean<>(200, "客户返回登记成功，床位状态已更新");
    }

}
